package com.cango.framework.base.util;

import org.apache.commons.codec.digest.Md5Crypt;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

public class MD5Util {
    public static String md5(String text){
        MessageDigest md;
        StringBuffer sb=new StringBuffer();
        try {
            md = MessageDigest.getInstance("MD5");

            md.update(text.getBytes("UTF-8"));
            byte[] pds=md.digest();
            for (byte pd : pds) {    //字节数组转换成十六进制字符串，形成最终的密文
                int v = pd & 0xff;
                if (v < 16) {
                    sb.append(0);
                }
                sb.append(Integer.toHexString(v));
            }
        } catch (Exception e) {
            return "";
        }
        return sb.toString();
    }

    public static String apr1Crypt(String pwd,String salt){
        try {
            return Md5Crypt.apr1Crypt(pwd.getBytes("UTF-8"), salt);
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }
}
